import 'package:app/pages/daily_work_report_module/my_daily_detail_module/my_daily_detail_view.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';
import '../top_dialog_page.dart';
import '../../../services/address.dart';
import '../../../services/dio_manager.dart';
import 'my_daily_model.dart';

class MyDailyController extends GetxController{



  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    requestData();
  }

  EasyRefreshController easyRefreshController = EasyRefreshController();

  TextEditingController editingController = TextEditingController();

  bool isTopShow = false;

  String replyType = '我的日报';

  var page = 1;

  var dataArr = [];

  /// 获取我的日报列表数据
  requestData({var reply_type, var search}) async {
    var params = {
      'search': search,
      'reply_type': '${reply_type}',
      // 'is_me':'',
      // 'report_type':'',
      // 'start_time':'',
      // 'end_time':'',
      'page': page,
      'limit': '10',
    };
    var json = await DioManager()
        .kkRequest(Address.reportList, params: params, method: 'get');

    MyDailyModel _model = MyDailyModel.fromJson(json);

    if (_model.data == null) {
      // dataArr.clear();
      print('暂无更多');
      easyRefreshController.finishLoad(noMore: true);
      // BotToast.showText(text: '暂无更多');
      update();
      // return;
    }

    if (page == 1) {
      easyRefreshController.resetLoadState();
      dataArr.clear();
      if(_model.data!=null){
        dataArr.addAll(_model.data!);
      }
    } else {
      if(_model.data!=null){
        dataArr.addAll(_model.data!);
      }
      if (_model.data == null) {
        easyRefreshController.finishLoad(noMore: true);
        BotToast.showText(text: '暂无更多');
      }
    }
    update();
  }

  /// 点击的弹框
  topClick()async{
    isTopShow = true;

    var a;
    if (replyType == '我的日报') {
      a = 0;
    }
    if (replyType == '评论我的') {
      a = 1;
    }
    if (replyType == '回复我的') {
      a = 2;
    }

    var data = await Get.dialog(TopDialogPage(a),
        barrierColor: Colors.transparent);
    if (data != null) {
      print(data);
      isTopShow = data['isShow'];
      replyType = data['text'];
      requestData(reply_type: data['reply_type']);
    }
    update();
  }

  ///下拉刷新
  pullToData()async{
    page = 1;
    requestData();
  }
  /// 上拉加载
  onLoadToData()async{
    page++;
    requestData();
  }
  /// 跳转页面
   pushToView(int index){
     MyDailyItemModel model = dataArr[index];

     // Get.to(MyDailyDetailView(),arguments: '${model.id}');
     Get.toNamed('/myDailyDetailDetail',arguments: '${model.id}');
   }

}